package cn.net.qinling.healthcare.remote;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;

import org.openhealthtools.mdht.uml.cda.ccd.ContinuityOfCareDocument;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;

import cn.net.qinling.healthcare.test.datagen.CCDGenerator;
import cn.net.qinling.healthcare.test.datagen.ExcelFileSelector;
import cn.net.qinling.healthcare.test.datagen.PrescriptionGenerator;
import cn.net.qinling.healthcare.test.datagen.Selector;

public class RemoteCDAGenerator extends UnicastRemoteObject implements
		RemoteGenerator {

	private CCDGenerator generator = null;

	private File[] pendingSources = null;

	private int currId = -1;

	private boolean isRandomMode = true;

	public RemoteCDAGenerator(CCDGenerator generator) throws RemoteException {
		this.generator = generator;
	}

	public RemoteCDAGenerator() throws RemoteException {
	}

	@Override
	public void enableRandomMode(boolean isRandomMode) throws RemoteException {
		this.isRandomMode = isRandomMode;
	}

	@Override
	public void feed(String sourceDir) throws RemoteException {
		File sourceFile = new File(sourceDir);
		if (sourceFile.isDirectory() && sourceFile.canRead()) {
			this.pendingSources = sourceFile.listFiles();
			currId = 0;
			this.isRandomMode = false;
		} else {
			throw new RemoteException("Data source is not ready!");
		}
	}

	private String loadXml(InputStream is) {
		StringBuffer sb = new StringBuffer();
		Scanner sc = null;
		try {
			sc = new Scanner(is);
			while (sc.hasNext()) {
				sb.append(sc.nextLine());

			}
		} finally {
			if (null != null) {
				sc.close();
			}
		}

		return sb.toString();
	}

	@Override
	public String next() throws RemoteException {
		if (isRandomMode) {
			ContinuityOfCareDocument doc = generator.generate();
			StringWriter writer = new StringWriter();
			try {
				CDAUtil.save(doc, writer);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return writer.getBuffer().toString();
		} else {
			if (pendingSources == null || currId >= pendingSources.length) {
				return null;
			}

			File sourceDir = pendingSources[currId];
			try {
				if (sourceDir.isFile()) {
					return loadXml(new FileInputStream(sourceDir));
				}
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
				return null;
			} finally {
				currId++;
			}
		}

		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LocateRegistry.createRegistry(10991);
			Selector selector = ExcelFileSelector.getSelector("e:\\codes.xls");
			CCDGenerator gen = new PrescriptionGenerator(selector);
			RemoteCDAGenerator remoteGen = new RemoteCDAGenerator(gen);
			System.out.println(remoteGen);
			Naming.rebind("rmi://localhost:10991/cdagen", remoteGen);
			System.out.println("Bound");

		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
